package com.liang.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liang.common.lang.FunctionUtil;
import com.liang.common.lang.Result;
import com.liang.entity.CBlog;
import com.liang.entity.CComment;
import com.liang.entity.CCommentId;
import com.liang.service.CBlogService;
import com.liang.service.CCommentIdService;
import com.liang.service.CCommentService;
import lombok.AllArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 李昂
 * @since 2020-11-30
 */
@RestController
@RequestMapping("/blog")
@AllArgsConstructor
public class CBlogController {

    private CBlogService blogService;
    private CCommentService commentService;
    private CCommentIdService commentIdService;

    //写博客
    @PostMapping
    public Result WriteBlog(@RequestBody CBlog blog) {
        blog.setPraise(0);
        blog.setLookTotal(0);
        blog.setCommentTotal(0);
        blog.setPublicTime(FunctionUtil.formatDate(new Date()));
        if (blogService.WriteBlog(blog)) {
            return Result.succ();
        } else {
            return Result.fail();
        }
    }

    //查看blog
    @GetMapping
    public Result getBlog(@RequestParam("id") String id,@RequestParam(value = "type",required = false) String type) {
        return Result.succ(blogService.getBlog(id,type));
    }

    //查看blog container
    @GetMapping("/{bid}")
    public Result getContainer(@PathVariable("bid") int bid) {
        return Result.succ(blogService.getContainer(bid));
    }


    //删除blog
    @DeleteMapping("/{bid}")
    public Result deleteBlog(@PathVariable("bid") int bid) {
        if (blogService.deleteBlog(bid)) {
            return Result.succ();
        } else {
            return Result.fail();
        }

    }

    //修改blog内容
    @PutMapping("/{bid}")
    public Result modifyBlog(@PathVariable("bid") int bid, @RequestParam("title") String title, @RequestParam("container") String container,@RequestParam("type") String type) {

        if (blogService.modifyBlog(bid, title, container,type)) {
            return Result.succ();
        } else {
            return Result.fail();
        }
    }

    //观看总数 -- 使用乐观锁机制
    @PutMapping("/look/{bid}")
    public Result lookBlog(@PathVariable("bid") int bid) {
        if (blogService.lookBlog(bid)) {
            return Result.succ();
        } else {
            return Result.fail();
        }
    }

    //修改点赞总数
    @PutMapping("/praise/{bid}")
    public Result Praise(@PathVariable("bid") int bid, @RequestParam("id") String id) {
        if (blogService.Praise(bid, id)) {
            return Result.succ();
        } else {
            return Result.fail();
        }
    }

    //确定一下此用户是否已经点过赞了
    @GetMapping("/praise/{bid}")
    public Result getPraise(@PathVariable("bid") int bid, @RequestParam("id") String id) {
        return Result.succ(blogService.getPraise(bid, id));
    }

    //得到7天blog数据
    @GetMapping("/data/{id}")
    public Result getHistoryBlogData(@PathVariable("id") String id) {
        return Result.succ(blogService.getHistoryBlogData(id));
    }

    //查看blog类型的观看数，评论数，点赞
    @GetMapping("/type/{id}")
    public Result getBlogType(@PathVariable("id") String id) {
        return Result.succ(blogService.getBlogType(id));
    }


    //游客可查看的blog页面
    @GetMapping("/page/{state}")
    public Result selectPageBlog(@PathVariable("state") Integer state) {
        Page<CBlog> page = new Page<>(state, 20, true);
        return Result.succ(blogService.selectPageBlog(page, state));
    }


    // ------------blog评论功能-------------


    //添加评论
    @PostMapping("/comment")
    public Result InsertBlogComment(@RequestBody CComment comment, @RequestParam(value = "cid", required = false) String cid) {

        String uid = FunctionUtil.uuid();


        //传过来的cid不为空说明是二级评论
        if (cid != null) {
            commentIdService.InsertCommentId(new CCommentId(comment.getBid(), cid, uid));
        }
        comment.setCid(uid);
        comment.setDatetime(FunctionUtil.formatDate(new Date()));
        if (commentService.InsertBlogComment(comment)) {
            return Result.succ();
        } else {
            return Result.fail();
        }

    }

    //查看评论
    @GetMapping("/comment/{bid}")
    public Result getBlogComment(@PathVariable("bid") Integer bid) {
        return Result.succ(commentService.getBlogComment(bid));
    }

    //查看评论id
    @GetMapping("/commentid/{bid}")
    public Result getCommentId(@PathVariable("bid") Integer bid) {
        return Result.succ(commentIdService.getCommentId(bid));
    }

    //删除评论  cid
    @DeleteMapping("/comment")
    @Transactional
    public Result DeleteComment(@RequestParam("cid") String cid) {

        if (commentIdService.deleteComment(cid) && commentService.deleteComment(cid)) {
            return Result.succ();
        } else {
            return Result.fail();
        }

    }

    //删除子评论 cid
    @DeleteMapping("/commentid")
    @Transactional
    public Result DeleteCommentid(@RequestParam("cid") String cid) {
        if (commentService.deleteCommentid(cid) || commentIdService.deleteCommentid(cid)) {
            return Result.succ();
        } else {
            return Result.fail();
        }
    }


    //评论总数 -- 使用乐观锁机制
    @PutMapping("/comment/{bid}")
    public Result CommentTotal(@PathVariable("bid") Integer bid) {
        if (blogService.commentTotal(bid)) {
            return Result.succ();
        } else {
            return Result.fail();
        }
    }


    //----------------  blog搜索功能  --------------
    @GetMapping("/search")
    public Result BlogSearch(@RequestParam("title") String title) {
        return Result.succ(blogService.BlogSearch(title));
    }





}
